package com.tj.jinzao.presenter;


import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.common.util.log.LogUtil;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.tj.jinzao.activity.CompleteInfoActivity;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.IRegisterLoginView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.User;
import com.tj.jinzao.utils.ToastUtils;

import java.util.HashMap;

/**
 * 登录注册处理
 * Created by wjy 2019/1/5.
 */
public class RegisterLoginPresenter extends BasePresenter<IRegisterLoginView<User>> {

    public RegisterLoginPresenter(IRegisterLoginView<User> view) {
        super(view);
    }

    public void sendCode(String phone, String type) {
        addSubscription(mApiService.sendCode(phone, type), new SubscriberCallBack<Boolean>(mView.getContext()) {
            @Override
            protected void onSuccess(Boolean response) {
                if (mView != null) mView.onGetCodeSuccess("");
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onGetCodeFailed(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    public void loginBySmsCode(String phone, String code) {
        addSubscription(mApiService.loginBySmsCode(phone, code), new SubscriberCallBack<User>(mView.getContext()) {
            @Override
            protected void onSuccess(User user) {
                UserManager.getInstance().saveUserData(user);
                loginChat(user);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    public void loginChat(final User user) {
        Log.w("数据用户信息",""+user.toString());
        NimUIKit.login(new LoginInfo(String.valueOf(user.getYunxinAccount()), "123456"), new RequestCallback<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo param) {
                LogUtil.i("wjy", "login success");
                // 初始化消息提醒配置
                initNotificationConfig();
                // 进入主界面
                if (mView != null) mView.onRequestSuccess(user);
            }

            @Override
            public void onFailed(int code) {

                if (mView != null && mView.getContext() != null) {
                    if (code == 302 || code == 404) {
                        ToastUtils.showToast(mView.getContext(), "帐号或密码错误");
                    } else {
                        ToastUtils.showToast(mView.getContext(), "登录失败: " + code);
                    }
                }
            }

            @Override
            public void onException(Throwable exception) {
                if (mView != null && mView.getContext() != null) {
                    ToastUtils.showToast(mView.getContext(), "login_exception");
                }
            }
        });
        updateUserInfo(user);
    }

    private void initNotificationConfig() {
        // 初始化消息提醒
        NIMClient.toggleNotification(true);
    }

    public void loginByPassword(String phone, String password) {
        addSubscription(mApiService.loginByPassword(phone, password), new SubscriberCallBack<User>(mView.getContext()) {
            @Override
            protected void onSuccess(User user) {
                UserManager.getInstance().saveUserData(user);
                loginChat(user);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    public void getUserByRequestCode(String code) {
        addSubscription(mApiService.getUserByRequestCode(code), new SubscriberCallBack<User>(mView.getContext()) {
            @Override
            protected void onSuccess(User user) {
                if (mView != null) mView.onRequestSuccess(user);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    public void bindRequestCode(final String code) {
        long id = UserManager.getInstance().getUser().getId();
        Log.w("数据接收",code+"--"+id);
        addSubscription(mApiService.bindRequestCode(id, code), new SubscriberCallBack<String>(mView.getContext()) {
            @Override
            protected void onSuccess(String string) {

                UserManager.getInstance().getUser().setRequestCode(code);
                Log.w("数据接收","dddd"+string);
                if (mView != null) mView.onGetCodeSuccess(string);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }


    public void logout() {
        UserManager.userLogout();
    }

    public void setPassword(String password) {
        long id = UserManager.getInstance().getUser().getId();
        HashMap<String, Object> map = new HashMap();
        map.put("password",password);
        addSubscription(mApiService.setPassword(id, map), new SubscriberCallBack<String>(mView.getContext()) {
            @Override
            protected void onSuccess(String string) {
                if (mView != null) mView.onRequestSuccess(null);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    public void resetPassword(String token, String phone, String smsCode, String password) {
        Log.w("重置密码",""+token+"--"+phone);
        addSubscription(mApiService.resetPassword(token, phone, smsCode, password), new SubscriberCallBack<String>(mView.getContext()) {
            @Override
            protected void onSuccess(String string) {
                Log.w("设置密码qq",""+string);
                if (mView != null) mView.onRequestSuccess(null);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                Log.w("设置密码www",""+response.getMessage());
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    public void validateSmsCode(String phone, String smsCode) {
        addSubscription(mApiService.validateCode(phone, smsCode), new SubscriberCallBack<String>(mView.getContext()) {
            @Override
            protected void onSuccess(String string) {
                if (mView != null) mView.onCheckCodeSuccess(string);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    public void getUserByOpenId(String openId) {
        addSubscription(mApiService.getUserByOpenId(openId), new SubscriberCallBack<User>(mView.getContext()) {
            @Override
            protected void onSuccess(User user) {
                if (mView != null) mView.onRequestSuccess(user);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

    public void bindPhone(String phone, String code, String password, Context context,int type) {
        long id = UserManager.getInstance().getUser().getId();
        HashMap<String,Object> map=new HashMap<>();
        map.put("mobile",phone);
        map.put("smsCode",code);
        map.put("password",password);
        addSubscription(mApiService.getBindPhone(id, map), new SubscriberCallBack<User>(mView.getContext()) {
            @Override
            protected void onSuccess(User user) {
//                if(type==6){
//                    if (mView != null) mView.onRequestSuccess(user);
//                }else {
                    context.startActivity(new Intent(context, CompleteInfoActivity.class));

//                }
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
            @Override
            protected void onStringNull(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }
        });
    }

}
